package com.booksys.examination.service.impl;

import com.booksys.examination.entity.BookInfo;
import com.booksys.examination.entity.PageBean;
import com.booksys.examination.mappers.BookInfoMapper;
import com.booksys.examination.service.BookInfoService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author Administrator
 * @date 2019-08-13 17:41
 */
@Service
public class BookServiceImpl implements BookInfoService {
    @Resource
    private BookInfoMapper bookInfoMapper;
    @Override
    public List<BookInfo> showAll() {
        return bookInfoMapper.findBookList();
    }

    @Override
    public PageBean<BookInfo> doPagination(Integer typeId,String title,Integer isBorrow ,int currentPage,int pageSize) {
        PageBean<BookInfo> pageBean = new PageBean<BookInfo>();
        int start = (currentPage - 1) * pageSize;
        pageBean.setStart(start);

        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);

        int totalCount = bookInfoMapper.findTotalCount(typeId,title,isBorrow);
        pageBean.setTotalCount(totalCount);

        int totalPage = (totalCount%pageSize==0 )? (totalCount/pageSize):(totalCount/pageSize+1);
        pageBean.setTotalPage(totalPage);

        List<BookInfo> bookByPage = bookInfoMapper.findBookByPage(typeId,title,isBorrow,start,pageSize);
        pageBean.setList(bookByPage);

        pageBean.setEnd(totalPage);

        return pageBean;
    }



}
